package cc.rengu.redp.bizimpl.upmp.service.impl;


import cc.rengu.redp.bizimpl.upmp.dao.NtcAdvMchntAreaOrgMapper;
import cc.rengu.redp.bizimpl.upmp.domain.NtcAdvMchntAreaOrg;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpOrganization;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpOrganizationService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.service.NtcAdvMchntAreaOrgservice;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 刘玉亮.LL
 */
@Slf4j
@Service
public class NtcAdvMchntAreaOrglmpl extends ServiceImpl<NtcAdvMchntAreaOrgMapper, NtcAdvMchntAreaOrg> implements NtcAdvMchntAreaOrgservice {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    public OperationResult saveData(NtcAdvMchntAreaOrg entity, String optFlag, String id, IUpmpOrganizationService organizationService) {
        String flag = optFlag;
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        Map<String, Object> result = com.google.common.collect.Maps.newHashMap();

        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String instId = user.getInstId();

        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        // 去掉"-"符号
        String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
        entity.setId(temp);
        String crtOrgId = "";
        if (user.isAdmin()) {
            entity.setInstId(instId);
            UpmpOrganization headOrg = organizationService.getHeadOrgByCode(instId);
            crtOrgId = headOrg == null ? user.getOrgCode() : headOrg.getOrgCode();
        } else {
            entity.setInstId(user.getInstId());
            crtOrgId = user.getOrgCode();
        }
        entity.setCrtOrgId(crtOrgId);
        entity.setOprId(user.getUserId());
        entity.setCreateTm(lastUpddt);
        entity.setInstId(instId);
        this.save(entity);
        result.put("id", entity.getId());

        return OperationResult.buildSuccessResult("数据保存处理完成", result);
    }

    public List<NtcAdvMchntAreaOrg> queryMerchantListAndCircleList(String id) {
        QueryWrapper<NtcAdvMchntAreaOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("NTCADV_NO", id);
        //删除关联表中的数据
        return this.list(queryWrapper);
    }

    public void deleteMerchantListAndCircle(String id) {
        QueryWrapper<NtcAdvMchntAreaOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("NTCADV_NO", id);
        //删除关联表中的数据
        this.remove(queryWrapper);
    }
}
